from app import ColumnModel
from app.serve.wtrees.db import db
from app.util.exception import WtreesError, ErrorType
from app.util.page import Page


class ColumnServe(ColumnModel):

    def column_create(self, column_name):
        self.create(column_name=column_name)
        db.session.commit()

    def verify(self, column_name):
        column = self.query.filter_by(column_name=column_name).first()
        if column:
            raise WtreesError(ErrorType.column_name_not)

    """
     栏目列表，保函分页和模糊搜索名字
    """

    def list(self, column_name):
        column = self.query
        if column_name:
            column = column.filter(self.column_name.like("%{}%".format(column_name)))
        page = Page(column)
        return page
